/**
 * Copyright @ 2000 ysten Co. Ltd.
 * All right reserved.
 * @author: wangmin
 * date: 2016-05-13
 */

$(function () {
    var url;
    $('#grid').datagrid({
        url: 'findBrandPage.json',
        singleSelect: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        shadow: false,
        columns: [
            [
                {title: '<b>主键</b>', field: 'id', align: 'center', width: 50, sort: 'clientSide'},
                {title: '<b>名称</b>', field: 'name', align: 'center', width: 150, sort: 'clientSide'},
                {title: '<b>备注</b>', field: 'description', align: 'center', width: 250, sort: 'clientSide'}
            ]
        ]
    });

    $("#queryBrandBtn").click(function () {
        $('#grid').datagrid('load', {
            search_LIKE_name: $("#search_LIKE_name").textbox("getValue"),
            search_LIKE_description: $("#search_LIKE_description").textbox("getValue")
        });
    });

    $("#addBrand").click(function () {
        $('#dlg').dialog({
            title: '新增品牌',
            modal: true
        });
        $('#dlg').dialog('open').dialog('center');
        $('#fm').form('clear');
        url = 'saveBrand.json';
    });

    $("#editBrand").click(function(){
        var rows = $('#grid').datagrid('getSelections');
        if (rows && rows.length == 1) {
            $('#fm').form('clear');
            $("#id").val(rows[0].id);
            $.get("findBrandById.json?id=" + rows[0].id, function (data) {
                if(data){
                    $('#dlg').dialog('open').dialog('center').dialog('setTitle', '修改品牌');
                    $('#fm').form('load', data);
                }
            });
            url = 'updateBrand.json';
        } else {
            EGIFT_APP.showEditWarnFunc();
        }
    });

    $("#submitBrandBtn").click(function(){
        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                var result = $(this).form('validate');
                if(result) {
                    $('#dlg').dialog('close');
                }
                return result;
            },
            success: function (data) {
                var obj = JSON.parse(data);
                if (obj.result) {
                    $.messager.show({
                        title: '成功',
                        msg: obj.msg
                    });
                } else {
                    $.messager.show({
                        title: '失败',
                        msg: obj.msg
                    });
                }
                $('#grid').datagrid('reload');
            }
        });
    });

    $("#deleteBrand").click(function(){
        var rows = $('#grid').datagrid('getSelections');
        if (rows && rows.length > 0) {
            $.messager.confirm('确认', '确认要删除吗？', function (r) {
                if (r) {
                    $.post('deleteBrand.json', {id: rows[0].id}, function (result) {
                        if (result == "success") {
                            $.messager.show({
                                title: '成功',
                                msg: "删除品牌成功"
                            });
                            $('#grid').datagrid('reload');    // reload the user data
                        } else {
                            $.messager.show({
                                title: '失败',
                                msg: "删除品牌失败"
                            });
                        }
                    });
                }
            });
        } else {
            EGIFT_APP.showDeleteWarnFunc();
        }
    });


})
